from browser import document, ajax, html
from browser.widgets.dialog import InfoDialog

url = "http://127.0.0.1:5000/recite/"
url2 = "http://127.0.0.1:5000/recite2/"
msg = "Position of the International Space Station at {}: {}"

recite1 = []
reciteIndex = 0
my_contents = []
 
def liChange(event):
    for i in my_contents:
        if i["title"] == event.target.text:
            document["textarea1"].textContent = i["content"]

def complete_contents(request):
    global my_contents
    data = request.json
    my_contents = data
    document["contents"] <= html.UL(html.LI(i["title"]) for i in my_contents)
    choices = [i["title"] for i in my_contents]
    for i in document.select("li"):
        i.bind("click",liChange)


ajax.get("http://127.0.0.1:5000/static/contents.json", oncomplete=complete_contents, mode="json")

def complete(request):
    global recite1
    data = request.json
    list = data["list"]
    recite1 = list

def click(event):
    global reciteIndex
    #document["textarea1"].style.visibility = "hidden"
    text = document["textarea1"].value.replace("\n","0000")
    reciteIndex = 0
    mode = document["inputGroupSelect"].value
    ajax.get("http://127.0.0.1:5000/recite/"+mode+"/"+text, oncomplete=complete, mode="json")

document["button10"].bind("click", click)



def click_for_button2(event):
    global recite1
    global reciteIndex
    textLen = len(recite1)
    if reciteIndex == textLen-1:
        document["textarea1"].value = recite1[reciteIndex]
        reciteIndex = 0
    else:
        
        document["textarea1"].value = recite1[reciteIndex]
        reciteIndex += 1
        
document["button2"].bind("click", click_for_button2)


